package com.Entity.Product;

import java.util.Iterator;
import java.util.List;

import com.DAO.Product_tagMap;
import com.DAO.Product_tagName;
import com.Entity.Basic;

public class Product_tag extends Basic implements Product_tagImplement {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title Product_tag.java
	 * @Package com.Entity
	 * @author ufo
	 */

	private String product_tagNameId;
	private String product_tagName;

	public static List iteratorTags(String max, String first) {
		return databaseControler.hibernateSess
				.createQuery(
						"select product_tagNameId from Product_tagName order by  product_tagNameEnabledNumber desc")
				.setFirstResult(Integer.parseInt(first))
				.setMaxResults(Integer.parseInt(max)).list();
	}

	public static List loadProduct(ProductImplement product) {
		return databaseControler.hibernateSess
				.createQuery(
						"select product_tagNameId from Product_tagMap where productId=:productId")
				.setString("productId", product.getProductId()).list();
	}

	public static Product_tag loadByProduct_tagName(String product_tagName) {
		if (product_tagNameIsExist(product_tagName))
			return new Product_tag(
					(String) databaseControler.hibernateSess
							.createQuery(
									"select product_tagNameId from Product_tagName where product_tagName=:product_tagName")
							.setString("product_tagName", product_tagName)
							.list().iterator().next());
		else{
			com.DAO.Product_tagName tagName=new com.DAO.Product_tagName();
			tagName.setProduct_tagName(product_tagName);
			tagName.setProduct_tagNameEnabledNumber(0);
			databaseControler.save(tagName);
			databaseControler.commit();
			return new Product_tag(tagName.getProduct_tagNameId());
		}
	}

	public Product_tag(String product_tagNameId) {
		com.DAO.Product_tagName tagName = (Product_tagName) databaseControler
				.load(com.DAO.Product_tagName.class, product_tagNameId);
		this.product_tagName = tagName.getProduct_tagName();
		this.product_tagNameId = tagName.getProduct_tagNameId();
	}

	public void connectWithProduct(ProductImplement product) {
		Iterator i = databaseControler.hibernateSess
				.createQuery(
						"from Product_tagMap where product_tagNameId=:product_tagNameId and productId=:productId")
				.setString("product_tagNameId", this.product_tagNameId)
				.setString("productId", product.getProductId())
				.list()
				.iterator();

		com.DAO.Product_tagMap tagMap;
		if (i.hasNext())
			tagMap = (Product_tagMap) i.next();
		else {
			tagMap = new com.DAO.Product_tagMap();
			tagMap.setProductId(product.getProductId());
			tagMap.setProduct_tagNameId(this.product_tagNameId);
			databaseControler.save(tagMap);
			databaseControler.commit();
		}

		com.DAO.Product_tagName tagName=(Product_tagName) databaseControler.load(com.DAO.Product_tagName.class, this.product_tagNameId);
		tagName.setProduct_tagNameEnabledNumber(tagName.getProduct_tagNameEnabledNumber()+1);
		databaseControler.save(tagName);
		databaseControler.commit();
	}

	public static boolean product_tagNameIsExist(String product_tagName) {
		List list = databaseControler.hibernateSess
				.createQuery(
						"from Product_tagName where product_tagName=:product_tagName")
				.setString("product_tagName", product_tagName).list();
		return !list.isEmpty();
	}

	public List getProducts() {
		return databaseControler.hibernateSess
				.createQuery(
						"seletc productId from Product_tagMap where product_tagNameId=:product_tagNameId")
				.setString("product_tagNameId", this.product_tagNameId).list();
	}

	public List getProductId(String max,String first) {
		return databaseControler.hibernateSess
				.createQuery(
						"select productId from Product_tagMap where product_tagNameId=:product_tagNameId")
				.setString("product_tagNameId", this.product_tagNameId)
				.setFirstResult(Integer.parseInt(first))
				.setMaxResults(Integer.parseInt(max))
				.list();
	}

	public String getProduct_tagNameId() {
		return product_tagNameId;
	}

	public void setProduct_tagNameId(String product_tagNameId) {
		this.product_tagNameId = product_tagNameId;
	}

	public String getProduct_tagName() {
		return product_tagName;
	}

	public void setProduct_tagName(String product_tagName) {
		this.product_tagName = product_tagName;
	}
}
